home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tcsh / dist / sh.lex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-21  |  33.1 KB  |  1,739 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.01/RCS/sh.lex.c,v 3.15 1991/12/19 22:34:14 christos Exp $ */
  2. /*
  3.  * sh.lex.c: Lexical analysis into tokens
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "sh.h"
  38.  
  39. RCSID("$Id: sh.lex.c,v 3.15 1991/12/19 22:34:14 christos Exp $")
  40.  
  41. #include "ed.h"
  42. /* #define DEBUG_INP */
  43. /* #define DEBUG_SEEK */
  44.  
  45. /*
  46.  * C shell
  47.  */
  48.  
  49. /*
  50.  * These lexical routines read input and form lists of words.
  51.  * There is some involved processing here, because of the complications
  52.  * of input buffering, and especially because of history substitution.
  53.  */
  54. static    Char        *word        __P((void));
  55. static    int          getC1        __P((int));
  56. static    void          getdol        __P((void));
  57. static    void          getexcl    __P((int));
  58. static    struct Hist     *findev        __P((Char *, bool));
  59. static    void          setexclp    __P((Char *));
  60. static    int          bgetc        __P((void));
  61. static    void          bfree        __P((void));
  62. static    struct wordent    *gethent    __P((int));
  63. static    int          matchs        __P((Char *, Char *));
  64. static    int          getsel        __P((int *, int *, int));
  65. static    struct wordent    *getsub        __P((struct wordent *));
  66. static    Char         *subword    __P((Char *, int, bool *));
  67. static    struct wordent    *dosub        __P((int, struct wordent *, bool));
  68.  
  69. /*
  70.  * Peekc is a peek character for getC, peekread for readc.
  71.  * There is a subtlety here in many places... history routines
  72.  * will read ahead and then insert stuff into the input stream.
  73.  * If they push back a character then they must push it behind
  74.  * the text substituted by the history substitution.  On the other
  75.  * hand in several places we need 2 peek characters.  To make this
  76.  * all work, the history routines read with getC, and make use both
  77.  * of ungetC and unreadc.  The key observation is that the state
  78.  * of getC at the call of a history reference is such that calls
  79.  * to getC from the history routines will always yield calls of
  80.  * readc, unless this peeking is involved.  That is to say that during
  81.  * getexcl the variables lap, exclp, and exclnxt are all zero.
  82.  *
  83.  * Getdol invokes history substitution, hence the extra peek, peekd,
  84.  * which it can ungetD to be before history substitutions.
  85.  */
  86. static Char peekc = 0, peekd = 0;
  87. static Char peekread = 0;
  88.  
  89. /* (Tail of) current word from ! subst */
  90. static Char *exclp = NULL;
  91.  
  92. /* The rest of the ! subst words */
  93. static struct wordent *exclnxt = NULL;
  94.  
  95. /* Count of remaining words in ! subst */
  96. static int exclc = 0;
  97.  
  98. /* "Globp" for alias resubstitution */
  99. int aret = F_SEEK;
  100.  
  101. /*
  102.  * Labuf implements a general buffer for lookahead during lexical operations.
  103.  * Text which is to be placed in the input stream can be stuck here.
  104.  * We stick parsed ahead $ constructs during initial input,
  105.  * process id's from `$$', and modified variable values (from qualifiers
  106.  * during expansion in sh.dol.c) here.
  107.  */
  108. static Char labuf[BUFSIZE];
  109.  
  110. /*
  111.  * Lex returns to its caller not only a wordlist (as a "var" parameter)
  112.  * but also whether a history substitution occurred.  This is used in
  113.  * the main (process) routine to determine whether to echo, and also
  114.  * when called by the alias routine to determine whether to keep the
  115.  * argument list.
  116.  */
  117. static bool hadhist = 0;
  118.  
  119. /*
  120.  * Avoid alias expansion recursion via \!#
  121.  */
  122. int     hleft;
  123.  
  124. Char    histline[BUFSIZE + 2];    /* last line input */
  125.  
  126.  /* The +2 is to fool hp's optimizer */
  127. bool    histvalid = 0;        /* is histline valid */
  128. static Char *histlinep = NULL;    /* current pointer into histline */
  129.  
  130. static Char getCtmp;
  131.  
  132. #define getC(f)        ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f))
  133. #define    ungetC(c)    peekc = c
  134. #define    ungetD(c)    peekd = c
  135.  
  136. int
  137. lex(hp)
  138.     register struct wordent *hp;
  139. {
  140.     register struct wordent *wdp;
  141.     int     c;
  142.  
  143.     histvalid = 0;
  144.     histlinep = histline;
  145.     *histlinep = '\0';
  146.  
  147.     btell(&lineloc);
  148.     hp->next = hp->prev = hp;
  149.     hp->word = STRNULL;
  150.     hadhist = 0;
  151.     do
  152.     c = readc(0);
  153.     while (c == ' ' || c == '\t');
  154.     if (c == HISTSUB && intty)
  155.     /* ^lef^rit    from tty is short !:s^lef^rit */
  156.     getexcl(c);
  157.     else
  158.     unreadc(c);
  159.     wdp = hp;
  160.     /*
  161.      * The following loop is written so that the links needed by freelex will
  162.      * be ready and rarin to go even if it is interrupted.
  163.      */
  164.     do {
  165.     register struct wordent *new;
  166.  
  167.     new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
  168.     new->word = 0;
  169.     new->prev = wdp;
  170.     new->next = hp;
  171.     wdp->next = new;
  172.     wdp = new;
  173.     wdp->word = word();
  174.     } while (wdp->word[0] != '\n');
  175.     hp->prev = wdp;
  176.     if (histlinep < histline + BUFSIZE) {
  177.     *histlinep = '\0';
  178.     if (histlinep > histline && histlinep[-1] == '\n')
  179.         histlinep[-1] = '\0';
  180.     histvalid = 1;
  181.     }
  182.     else {
  183.     histline[BUFSIZE - 1] = '\0';
  184.     }
  185.  
  186.     return (hadhist);
  187. }
  188.  
  189. void
  190. prlex(sp0)
  191.     struct wordent *sp0;
  192. {
  193.     register struct wordent *sp = sp0->next;
  194.  
  195.     for (;;) {
  196.     xprintf("%s", short2str(sp->word));
  197.     sp = sp->next;
  198.     if (sp == sp0)
  199.         break;
  200.     if (sp->word[0] != '\n')
  201.         xputchar(' ');
  202.     }
  203. }
  204.  
  205. void
  206. copylex(hp, fp)
  207.     register struct wordent *hp;
  208.     register struct wordent *fp;
  209. {
  210.     register struct wordent *wdp;
  211.  
  212.     wdp = hp;
  213.     fp = fp->next;
  214.     do {
  215.     register struct wordent *new;
  216.     
  217.     new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
  218.     new->prev = wdp;
  219.     new->next = hp;
  220.     wdp->next = new;
  221.     wdp = new;
  222.     wdp->word = Strsave(fp->word);
  223.     fp = fp->next;
  224.     } while (wdp->word[0] != '\n');
  225.     hp->prev = wdp;
  226. }
  227.  
  228. void
  229. freelex(vp)
  230.     register struct wordent *vp;
  231. {
  232.     register struct wordent *fp;
  233.  
  234.     while (vp->next != vp) {
  235.     fp = vp->next;
  236.     vp->next = fp->next;
  237.     xfree((ptr_t) fp->word);
  238.     xfree((ptr_t) fp);
  239.     }
  240.     vp->prev = vp;
  241. }
  242.  
  243. static Char *
  244. word()
  245. {
  246.     register Char c, c1;
  247.     register Char *wp;
  248.     Char    wbuf[BUFSIZE];
  249.     register bool dolflg;
  250.     register int i;
  251.  
  252.     wp = wbuf;
  253.     i = BUFSIZE - 4;
  254. loop:
  255.     while ((c = getC(DOALL)) == ' ' || c == '\t');
  256.     if (cmap(c, _META | _ESC))
  257.     switch (c) {
  258.     case '&':
  259.     case '|':
  260.     case '<':
  261.     case '>':
  262.         *wp++ = c;
  263.         c1 = getC(DOALL);
  264.         if (c1 == c)
  265.         *wp++ = c1;
  266.         else
  267.         ungetC(c1);
  268.         goto ret;
  269.  
  270.     case '#':
  271.         if (intty)
  272.         break;
  273.         c = 0;
  274.         do {
  275.         c1 = c;
  276.         c = getC(0);
  277.         } while (c != '\n');
  278.         if (c1 == '\\')
  279.         goto loop;
  280.         /* fall into ... */
  281.  
  282.     case ';':
  283.     case '(':
  284.     case ')':
  285.     case '\n':
  286.         *wp++ = c;
  287.         goto ret;
  288.  
  289.     case '\\':
  290.         c = getC(0);
  291.         if (c == '\n') {
  292.         if (onelflg == 1)
  293.             onelflg = 2;
  294.         goto loop;
  295.         }
  296.         if (c != HIST)
  297.         *wp++ = '\\', --i;
  298.         c |= QUOTE;
  299.     default:
  300.         break;
  301.     }
  302.     c1 = 0;
  303.     dolflg = DOALL;
  304.     for (;;) {
  305.     if (c1) {
  306.         if (c == c1) {
  307.         c1 = 0;
  308.         dolflg = DOALL;
  309.         }
  310.         else if (c == '\\') {
  311.         c = getC(0);
  312. /*
  313.  * PWP: this is dumb, but how all of the other shells work.  If \ quotes
  314.  * a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY
  315.  * following character INSIDE a set of ''s.
  316.  *
  317.  * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
  318.  */
  319.         if (c == HIST)
  320.             c |= QUOTE;
  321.         else {
  322.             if (bslash_quote &&
  323.             ((c == '\'') || (c == '"') ||
  324.              (c == '\\'))) {
  325.             c |= QUOTE;
  326.             }
  327.             else {
  328.             if (c == '\n')
  329.                 /*
  330.                  * if (c1 == '`') c = ' '; else
  331.                  */
  332.                 c |= QUOTE;
  333.             ungetC(c);
  334.             c = '\\';
  335.             }
  336.         }
  337.         }
  338.         else if (c == '\n') {
  339.         seterror(ERR_UNMATCHED, c1);
  340.         ungetC(c);
  341.         break;
  342.         }
  343.     }
  344.     else if (cmap(c, _META | _Q | _Q1 | _ESC)) {
  345.         if (c == '\\') {
  346.         c = getC(0);
  347.         if (c == '\n') {
  348.             if (onelflg == 1)
  349.             onelflg = 2;
  350.             break;
  351.         }
  352.         if (c != HIST)
  353.             *wp++ = '\\', --i;
  354.         c |= QUOTE;
  355.         }
  356.         else if (cmap(c, _Q | _Q1)) {    /* '"` */
  357.         c1 = c;
  358.         dolflg = c == '"' ? DOALL : DOEXCL;
  359.         }
  360.         else if (c != '#' || !intty) {
  361.         ungetC(c);
  362.         break;
  363.         }
  364.     }
  365.     if (--i > 0) {
  366.         *wp++ = c;
  367.         c = getC(dolflg);
  368.     }
  369.     else {
  370.         seterror(ERR_WTOOLONG);
  371.         wp = &wbuf[1];
  372.         break;
  373.     }
  374.     }
  375. ret:
  376.     *wp = 0;
  377.     return (Strsave(wbuf));
  378. }
  379.  
  380. static int
  381. getC1(flag)
  382.     register int flag;
  383. {
  384.     register Char c;
  385.  
  386.     while (1) {
  387.     if (c = peekc) {
  388.         peekc = 0;
  389.         return (c);
  390.     }
  391.     if (lap) {
  392.         if ((c = *lap++) == 0)
  393.         lap = 0;
  394.         else {
  395.         if (cmap(c, _META | _Q | _Q1))
  396.             c |= QUOTE;
  397.         return (c);
  398.         }
  399.     }
  400.     if (c = peekd) {
  401.         peekd = 0;
  402.         return (c);
  403.     }
  404.     if (exclp) {
  405.         if (c = *exclp++)
  406.         return (c);
  407.         if (exclnxt && --exclc >= 0) {
  408.         exclnxt = exclnxt->next;
  409.         setexclp(exclnxt->word);
  410.         return (' ');
  411.         }
  412.         exclp = 0;
  413.         exclnxt = 0;
  414.     }
  415.     if (exclnxt) {
  416.         exclnxt = exclnxt->next;
  417.         if (--exclc < 0)
  418.         exclnxt = 0;
  419.         else
  420.         setexclp(exclnxt->word);
  421.         continue;
  422.     }
  423.     c = readc(0);
  424.     if (c == '$' && (flag & DODOL)) {
  425.         getdol();
  426.         continue;
  427.     }
  428.     if (c == HIST && (flag & DOEXCL)) {
  429.         getexcl(0);
  430.         continue;
  431.     }
  432.     break;
  433.     }
  434.     return (c);
  435. }
  436.  
  437. static void
  438. getdol()
  439. {
  440.     register Char *np, *ep;
  441.     Char    name[4 * MAXVARLEN + 1];
  442.     register int c;
  443.     int     sc;
  444.     bool    special = 0, toolong;
  445.  
  446.     np = name, *np++ = '$';
  447.     c = sc = getC(DOEXCL);
  448.     if (any("\t \n", c)) {
  449.     ungetD(c);
  450.     ungetC('$' | QUOTE);
  451.     return;
  452.     }
  453.     if (c == '{')
  454.     *np++ = c, c = getC(DOEXCL);
  455.     if (c == '#' || c == '?')
  456.     special++, *np++ = c, c = getC(DOEXCL);
  457.     *np++ = c;
  458.     switch (c) {
  459.  
  460.     case '<':
  461.     case '$':
  462.     case '!':
  463.     if (special)
  464.         seterror(ERR_SPDOLLT);
  465.     *np = 0;
  466.     addla(name);
  467.     return;
  468.  
  469.     case '\n':
  470.     ungetD(c);
  471.     np--;
  472.     seterror(ERR_NEWLINE);
  473.     *np = 0;
  474.     addla(name);
  475.     return;
  476.  
  477.     case '*':
  478.     if (special)
  479.         seterror(ERR_SPSTAR);
  480.     *np = 0;
  481.     addla(name);
  482.     return;
  483.  
  484.     default:
  485.     toolong = 0;
  486.     if (Isdigit(c)) {
  487. #ifdef notdef
  488.         /* let $?0 pass for now */
  489.         if (special) {
  490.         seterror(ERR_DIGIT);
  491.         *np = 0;
  492.         addla(name);
  493.         return;
  494.         }
  495. #endif
  496.         /* we know that np < &name[4] */
  497.         ep = &np[MAXVARLEN];
  498.         while (c = getC(DOEXCL)) {
  499.         if (!Isdigit(c))
  500.             break;
  501.         if (np < ep)
  502.             *np++ = c;
  503.         else
  504.             toolong = 1;
  505.         }
  506.     }
  507.     else if (letter(c)) {
  508.         /* we know that np < &name[4] */
  509.         ep = &np[MAXVARLEN];
  510.         toolong = 0;
  511.         while (c = getC(DOEXCL)) {
  512.         /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
  513.         if (!letter(c) && !Isdigit(c))
  514.             break;
  515.         if (np < ep)
  516.             *np++ = c;
  517.         else
  518.             toolong = 1;
  519.         }
  520.     }
  521.     else {
  522.         *np = 0;
  523.         seterror(ERR_VARILL);
  524.         addla(name);
  525.         return;
  526.     }
  527.     if (toolong) {
  528.         seterror(ERR_VARTOOLONG);
  529.         *np = 0;
  530.         addla(name);
  531.         return;
  532.     }
  533.     break;
  534.     }
  535.     if (c == '[') {
  536.     *np++ = c;
  537.     /*
  538.      * Name up to here is a max of MAXVARLEN + 8.
  539.      */
  540.     ep = &np[2 * MAXVARLEN + 8];
  541.     do {
  542.         /*
  543.          * Michael Greim: Allow $ expansion to take place in selector
  544.          * expressions. (limits the number of characters returned)
  545.          */
  546.         c = getC(DOEXCL | DODOL);
  547.         if (c == '\n') {
  548.         ungetD(c);
  549.         np--;
  550.         seterror(ERR_NLINDEX);
  551.         *np = 0;
  552.         addla(name);
  553.         return;
  554.         }
  555.         if (np < ep)
  556.         *np++ = c;
  557.     } while (c != ']');
  558.     *np = '\0';
  559.     if (np >= ep) {
  560.         seterror(ERR_SELOVFL);
  561.         addla(name);
  562.         return;
  563.     }
  564.     c = getC(DOEXCL);
  565.     }
  566.     /*
  567.      * Name up to here is a max of 2 * MAXVARLEN + 8.
  568.      */
  569.     if (c == ':') {
  570.     /*
  571.      * if the :g modifier is followed by a newline, then error right away!
  572.      * -strike
  573.      */
  574.  
  575.     int     gmodflag = 0, amodflag = 0;
  576.  
  577. #ifndef COMPAT
  578.     do {
  579. #endif /* COMPAT */
  580.         *np++ = c, c = getC(DOEXCL);
  581.         if (c == 'g' || c == 'a') {
  582.         if (c == 'g')
  583.             gmodflag++;
  584.         else
  585.             amodflag++;
  586.         *np++ = c; c = getC(DOEXCL);
  587.         }
  588.         if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
  589.         if (c == 'g')
  590.             gmodflag++;
  591.         else
  592.             amodflag++;
  593.         *np++ = c; c = getC(DOEXCL);
  594.         }
  595.         *np++ = c;
  596.         /* scan s// [eichin:19910926.0512EST] */
  597.         if (c == 's') {
  598.         int delimcnt = 2;
  599.         int delim = getC(0);
  600.         *np++ = delim;
  601.         
  602.         if (!delim || letter(delim)
  603.             || Isdigit(delim) || any(" \t\n", delim)) {
  604.             seterror(ERR_BADSUBST);
  605.             break;
  606.         }    
  607.         while ((c = getC(0)) != (-1)) {
  608.             *np++ = c;
  609.             if(c == delim) delimcnt--;
  610.             if(!delimcnt) break;
  611.         }
  612.         if(delimcnt) {
  613.             seterror(ERR_BADSUBST);
  614.             break;
  615.         }
  616.         c = 's';
  617.         }
  618.         if (!any("htrqxes", c)) {
  619.         if ((amodflag || gmodflag) && c == '\n')
  620.             stderror(ERR_VARSYN);    /* strike */
  621.         seterror(ERR_VARMOD, c);
  622.         *np = 0;
  623.         addla(name);
  624.         return;
  625.         }
  626. #ifndef COMPAT
  627.     }
  628.     while ((c = getC(DOEXCL)) == ':');
  629.     ungetD(c);
  630. #endif /* COMPAT */
  631.     }
  632.     else
  633.     ungetD(c);
  634.     if (sc == '{') {
  635.     c = getC(DOEXCL);
  636.     if (c != '}') {
  637.         ungetD(c);
  638.         seterror(ERR_MISSING, '}');
  639.         *np = 0;
  640.         addla(name);
  641.         return;
  642.     }
  643.     *np++ = c;
  644.     }
  645.     *np = 0;
  646.     addla(name);
  647.     return;
  648. }
  649.  
  650. void
  651. addla(cp)
  652.     Char   *cp;
  653. {
  654.     Char    buf[BUFSIZE];
  655.  
  656.     if (Strlen(cp) + (lap ? Strlen(lap) : 0) >=
  657.     (sizeof(labuf) - 4) / sizeof(Char)) {
  658.     seterror(ERR_EXPOVFL);
  659.     return;
  660.     }
  661.     if (lap)
  662.     (void) Strcpy(buf, lap);
  663.     (void) Strcpy(labuf, cp);
  664.     if (lap)
  665.     (void) Strcat(labuf, buf);
  666.     lap = labuf;
  667. }
  668.  
  669. static Char lhsb[32];
  670. static Char slhs[32];
  671. static Char rhsb[64];
  672. static int quesarg;
  673.  
  674. static void
  675. getexcl(sc)
  676.     int    sc;
  677. {
  678.     register struct wordent *hp, *ip;
  679.     int     left, right, dol;
  680.     register int c;
  681.  
  682.     if (sc == 0) {
  683.     sc = getC(0);
  684.     if (sc != '{') {
  685.         ungetC(sc);
  686.         sc = 0;
  687.     }
  688.     }
  689.     quesarg = -1;
  690.     lastev = eventno;
  691.     hp = gethent(sc);
  692.     if (hp == 0)
  693.     return;
  694.     hadhist = 1;
  695.     dol = 0;
  696.     if (hp == alhistp)
  697.     for (ip = hp->next->next; ip != alhistt; ip = ip->next)
  698.         dol++;
  699.     else
  700.     for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
  701.         dol++;
  702.     left = 0, right = dol;
  703.     if (sc == HISTSUB) {
  704.     ungetC('s'), unreadc(HISTSUB), c = ':';
  705.     goto subst;
  706.     }
  707.     c = getC(0);
  708.     if (!any(":^$*-%", c))
  709.     goto subst;
  710.     left = right = -1;
  711.     if (c == ':') {
  712.     c = getC(0);
  713.     unreadc(c);
  714.     if (letter(c) || c == '&') {
  715.         c = ':';
  716.         left = 0, right = dol;
  717.         goto subst;
  718.     }
  719.     }
  720.     else
  721.     ungetC(c);
  722.     if (!getsel(&left, &right, dol))
  723.     return;
  724.     c = getC(0);
  725.     if (c == '*')
  726.     ungetC(c), c = '-';
  727.     if (c == '-') {
  728.     if (!getsel(&left, &right, dol))
  729.         return;
  730.     c = getC(0);
  731.     }
  732. subst:
  733.     exclc = right - left + 1;
  734.     while (--left >= 0)
  735.     hp = hp->next;
  736.     if (sc == HISTSUB || c == ':') {
  737.     do {
  738.         hp = getsub(hp);
  739.         c = getC(0);
  740.     } while (c == ':');
  741.     }
  742.     unreadc(c);
  743.     if (sc == '{') {
  744.     c = getC(0);
  745.     if (c != '}')
  746.         seterror(ERR_BADBANG);
  747.     }
  748.     exclnxt = hp;
  749. }
  750.  
  751. static struct wordent *
  752. getsub(en)
  753.     struct wordent *en;
  754. {
  755.     register Char *cp;
  756.     int     delim;
  757.     register int c;
  758.     int     sc;
  759.     bool global;
  760.     Char    orhsb[sizeof(rhsb) / sizeof(Char)];
  761.  
  762. #ifndef COMPAT
  763.     do {
  764. #endif /* COMPAT */
  765.     exclnxt = 0;
  766.     global = 0;
  767.     sc = c = getC(0);
  768.     if (c == 'g' || c == 'a') {
  769.         global |= (c == 'g') ? 1 : 2;
  770.         sc = c = getC(0);
  771.     }
  772.     if (((c =='g') && !(global & 1)) || ((c == 'a') && !(global & 2))) {
  773.         global |= (c == 'g') ? 1 : 2;
  774.         sc = c = getC(0);
  775.     }
  776.  
  777.     switch (c) {
  778.     case 'p':
  779.         justpr++;
  780.         return (en);
  781.  
  782.     case 'x':
  783.     case 'q':
  784.         global |= 1;
  785.  
  786.         /* fall into ... */
  787.  
  788.     case 'h':
  789.     case 'r':
  790.     case 't':
  791.     case 'e':
  792.         break;
  793.  
  794.     case '&':
  795.         if (slhs[0] == 0) {
  796.         seterror(ERR_NOSUBST);
  797.         return (en);
  798.         }
  799.         (void) Strcpy(lhsb, slhs);
  800.         break;
  801.  
  802. #ifdef notdef
  803.     case '~':
  804.         if (lhsb[0] == 0)
  805.         goto badlhs;
  806.         break;
  807. #endif
  808.  
  809.     case 's':
  810.         delim = getC(0);
  811.         if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
  812.         unreadc(delim);
  813.         lhsb[0] = 0;
  814.         seterror(ERR_BADSUBST);
  815.         return (en);
  816.         }
  817.         cp = lhsb;
  818.         for (;;) {
  819.         c = getC(0);
  820.         if (c == '\n') {
  821.             unreadc(c);
  822.             break;
  823.         }
  824.         if (c == delim)
  825.             break;
  826.         if (cp > &lhsb[sizeof(lhsb) / sizeof(Char) - 2]) {
  827.             lhsb[0] = 0;
  828.             seterror(ERR_BADSUBST);
  829.             return (en);
  830.         }
  831.         if (c == '\\') {
  832.             c = getC(0);
  833.             if (c != delim && c != '\\')
  834.             *cp++ = '\\';
  835.         }
  836.         *cp++ = c;
  837.         }
  838.         if (cp != lhsb)
  839.         *cp++ = 0;
  840.         else if (lhsb[0] == 0) {
  841.         seterror(ERR_LHS);
  842.         return (en);
  843.         }
  844.         cp = rhsb;
  845.         (void) Strcpy(orhsb, cp);
  846.         for (;;) {
  847.         c = getC(0);
  848.         if (c == '\n') {
  849.             unreadc(c);
  850.             break;
  851.         }
  852.         if (c == delim)
  853.             break;
  854. #ifdef notdef
  855.         if (c == '~') {
  856.             if (&cp[Strlen(orhsb)] > &rhsb[sizeof(rhsb) /
  857.                            sizeof(Char) - 2])
  858.             goto toorhs;
  859.             (void) Strcpy(cp, orhsb);
  860.             cp = Strend(cp);
  861.             continue;
  862.         }
  863. #endif
  864.         if (cp > &rhsb[sizeof(rhsb) / sizeof(Char) - 2]) {
  865.             seterror(ERR_RHSLONG);
  866.             return (en);
  867.         }
  868.         if (c == '\\') {
  869.             c = getC(0);
  870.             if (c != delim /* && c != '~' */ )
  871.             *cp++ = '\\';
  872.         }
  873.         *cp++ = c;
  874.         }
  875.         *cp++ = 0;
  876.         break;
  877.  
  878.     default:
  879.         if (c == '\n')
  880.         unreadc(c);
  881.         seterror(ERR_BADBANGMOD, c);
  882.         return (en);
  883.     }
  884.     (void) Strcpy(slhs, lhsb);
  885.     if (exclc)
  886.         en = dosub(sc, en, global);
  887. #ifndef COMPAT
  888.     }
  889.     while ((c = getC(0)) == ':');
  890.     unreadc(c);
  891. #endif /* COMPAT */
  892.     return (en);
  893. }
  894.  
  895. static struct wordent *
  896. dosub(sc, en, global)
  897.     int     sc;
  898.     struct wordent *en;
  899.     bool global;
  900. {
  901.     struct wordent lexi;
  902.     bool    didsub = 0, didone = 0;
  903.     struct wordent *hp = &lexi;
  904.     register struct wordent *wdp;
  905.     register int i = exclc;
  906.  
  907.     wdp = hp;
  908.     while (--i >= 0) {
  909.     register struct wordent *new = 
  910.         (struct wordent *) xcalloc(1, sizeof *wdp);
  911.  
  912.     new->word = 0;
  913.     new->prev = wdp;
  914.     new->next = hp;
  915.     wdp->next = new;
  916.     wdp = new;
  917.     en = en->next;
  918.     if (en->word) {
  919.         Char *tword, *otword;
  920.  
  921.         if ((global & 1) || didsub == 0) {
  922.         tword = subword(en->word, sc, &didone);
  923.         if (didone)
  924.             didsub = 1;
  925.         if (global & 2) {
  926.             while (didone && tword != STRNULL) {
  927.             otword = tword;
  928.             tword = subword(otword, sc, &didone);
  929.             if (Strcmp(tword, otword) == 0) {
  930.                 xfree((ptr_t) otword);
  931.                 break;
  932.             }
  933.             else
  934.                 xfree((ptr_t) otword);
  935.             }
  936.         }
  937.         }
  938.         else
  939.         tword = Strsave(en->word);
  940.         wdp->word = tword;
  941.     }
  942.     }
  943.     if (didsub == 0)
  944.     seterror(ERR_MODFAIL);
  945.     hp->prev = wdp;
  946.     return (&enthist(-1000, &lexi, 0)->Hlex);
  947. }
  948.  
  949. static Char *
  950. subword(cp, type, adid)
  951.     Char   *cp;
  952.     int     type;
  953.     bool   *adid;
  954. {
  955.     Char    wbuf[BUFSIZE];
  956.     register Char *wp, *mp, *np;
  957.     register int i;
  958.  
  959.     *adid = 0;
  960.     switch (type) {
  961.  
  962.     case 'r':
  963.     case 'e':
  964.     case 'h':
  965.     case 't':
  966.     case 'q':
  967.     case 'x':
  968.     wp = domod(cp, type);
  969.     if (wp == 0)
  970.         return (Strsave(cp));
  971.     *adid = 1;
  972.     return (wp);
  973.  
  974.     default:
  975.     wp = wbuf;
  976.     i = BUFSIZE - 4;
  977.     for (mp = cp; *mp; mp++)
  978.         if (matchs(mp, lhsb)) {
  979.         for (np = cp; np < mp;)
  980.             *wp++ = *np++, --i;
  981.         for (np = rhsb; *np; np++)
  982.             switch (*np) {
  983.  
  984.             case '\\':
  985.             if (np[1] == '&')
  986.                 np++;
  987.             /* fall into ... */
  988.  
  989.             default:
  990.             if (--i < 0) {
  991.                 seterror(ERR_SUBOVFL);
  992.                 return (STRNULL);
  993.             }
  994.             *wp++ = *np;
  995.             continue;
  996.  
  997.             case '&':
  998.             i -= Strlen(lhsb);
  999.             if (i < 0) {
  1000.                 seterror(ERR_SUBOVFL);
  1001.                 return (STRNULL);
  1002.             }
  1003.             *wp = 0;
  1004.             (void) Strcat(wp, lhsb);
  1005.             wp = Strend(wp);
  1006.             continue;
  1007.             }
  1008.         mp += Strlen(lhsb);
  1009.         i -= Strlen(mp);
  1010.         if (i < 0) {
  1011.             seterror(ERR_SUBOVFL);
  1012.             return (STRNULL);
  1013.         }
  1014.         *wp = 0;
  1015.         (void) Strcat(wp, mp);
  1016.         *adid = 1;
  1017.         return (Strsave(wbuf));
  1018.         }
  1019.     return (Strsave(cp));
  1020.     }
  1021. }
  1022.  
  1023. Char   *
  1024. domod(cp, type)
  1025.     Char   *cp;
  1026.     int     type;
  1027. {
  1028.     register Char *wp, *xp;
  1029.     register int c;
  1030.  
  1031.     switch (type) {
  1032.  
  1033.     case 'x':
  1034.     case 'q':
  1035.     wp = Strsave(cp);
  1036.     for (xp = wp; c = *xp; xp++)
  1037.         if ((c != ' ' && c != '\t') || type == 'q')
  1038.         *xp |= QUOTE;
  1039.     return (wp);
  1040.  
  1041.     case 'h':
  1042.     case 't':
  1043.     if (!any(short2str(cp), '/'))
  1044.         return (type == 't' ? Strsave(cp) : 0);
  1045.     wp = Strend(cp);
  1046.     while (*--wp != '/')
  1047.         continue;
  1048.     if (type == 'h')
  1049.         xp = Strsave(cp), xp[wp - cp] = 0;
  1050.     else
  1051.         xp = Strsave(wp + 1);
  1052.     return (xp);
  1053.  
  1054.     case 'e':
  1055.     case 'r':
  1056.     wp = Strend(cp);
  1057.     for (wp--; wp >= cp && *wp != '/'; wp--)
  1058.         if (*wp == '.') {
  1059.         if (type == 'e')
  1060.             xp = Strsave(wp + 1);
  1061.         else
  1062.             xp = Strsave(cp), xp[wp - cp] = 0;
  1063.         return (xp);
  1064.         }
  1065.     return (Strsave(type == 'e' ? STRNULL : cp));
  1066.     default:
  1067.     break;
  1068.     }
  1069.     return (0);
  1070. }
  1071.  
  1072. static int
  1073. matchs(str, pat)
  1074.     register Char *str, *pat;
  1075. {
  1076.     while (*str && *pat && *str == *pat)
  1077.     str++, pat++;
  1078.     return (*pat == 0);
  1079. }
  1080.  
  1081. static int
  1082. getsel(al, ar, dol)
  1083.     register int *al, *ar;
  1084.     int     dol;
  1085. {
  1086.     register int c = getC(0);
  1087.     register int i;
  1088.     bool    first = *al < 0;
  1089.  
  1090.     switch (c) {
  1091.  
  1092.     case '%':
  1093.     if (quesarg == -1) {
  1094.         seterror(ERR_BADBANGARG);
  1095.         return (0);
  1096.     }
  1097.     if (*al < 0)
  1098.         *al = quesarg;
  1099.     *ar = quesarg;
  1100.     break;
  1101.  
  1102.     case '-':
  1103.     if (*al < 0) {
  1104.         *al = 0;
  1105.         *ar = dol - 1;
  1106.         unreadc(c);
  1107.     }
  1108.     return (1);
  1109.  
  1110.     case '^':
  1111.     if (*al < 0)
  1112.         *al = 1;
  1113.     *ar = 1;
  1114.     break;
  1115.  
  1116.     case '$':
  1117.     if (*al < 0)
  1118.         *al = dol;
  1119.     *ar = dol;
  1120.     break;
  1121.  
  1122.     case '*':
  1123.     if (*al < 0)
  1124.         *al = 1;
  1125.     *ar = dol;
  1126.     if (*ar < *al) {
  1127.         *ar = 0;
  1128.         *al = 1;
  1129.         return (1);
  1130.     }
  1131.     break;
  1132.  
  1133.     default:
  1134.     if (Isdigit(c)) {
  1135.         i = 0;
  1136.         while (Isdigit(c)) {
  1137.         i = i * 10 + c - '0';
  1138.         c = getC(0);
  1139.         }
  1140.         if (i < 0)
  1141.         i = dol + 1;
  1142.         if (*al < 0)
  1143.         *al = i;
  1144.         *ar = i;
  1145.     }
  1146.     else if (*al < 0)
  1147.         *al = 0, *ar = dol;
  1148.     else
  1149.         *ar = dol - 1;
  1150.     unreadc(c);
  1151.     break;
  1152.     }
  1153.     if (first) {
  1154.     c = getC(0);
  1155.     unreadc(c);
  1156.     if (any("-$*", c))
  1157.         return (1);
  1158.     }
  1159.     if (*al > *ar || *ar > dol) {
  1160.     seterror(ERR_BADBANGARG);
  1161.     return (0);
  1162.     }
  1163.     return (1);
  1164.  
  1165. }
  1166.  
  1167. static struct wordent *
  1168. gethent(sc)
  1169.     int     sc;
  1170. {
  1171.     register struct Hist *hp;
  1172.     register Char *np;
  1173.     register int c;
  1174.     int     event;
  1175.     bool    back = 0;
  1176.  
  1177.     c = sc == HISTSUB ? HIST : getC(0);
  1178.     if (c == HIST) {
  1179.     if (alhistp)
  1180.         return (alhistp);
  1181.     event = eventno;
  1182.     }
  1183.     else
  1184.     switch (c) {
  1185.  
  1186.     case ':':
  1187.     case '^':
  1188.     case '$':
  1189.     case '*':
  1190.     case '%':
  1191.         ungetC(c);
  1192.         if (lastev == eventno && alhistp)
  1193.         return (alhistp);
  1194.         event = lastev;
  1195.         break;
  1196.  
  1197.     case '#':        /* !# is command being typed in (mrh) */
  1198.         if (--hleft == 0) {
  1199.         seterror(ERR_HISTLOOP);
  1200.         return (0);
  1201.         }
  1202.         else
  1203.         return (¶ml);
  1204.         /* NOTREACHED */
  1205.  
  1206.     case '-':
  1207.         back = 1;
  1208.         c = getC(0);
  1209.         /* FALLSTHROUGH */
  1210.  
  1211.     default:
  1212.         if (any("(=~", c)) {
  1213.         unreadc(c);
  1214.         ungetC(HIST);
  1215.         return (0);
  1216.         }
  1217.         np = lhsb;
  1218.         event = 0;
  1219.         while (!cmap(c, _ESC | _META | _Q | _Q1) && !any("${}:#", c)) {
  1220.         if (event != -1 && Isdigit(c))
  1221.             event = event * 10 + c - '0';
  1222.         else
  1223.             event = -1;
  1224.         if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
  1225.             *np++ = c;
  1226.         c = getC(0);
  1227.         }
  1228.         unreadc(c);
  1229.         if (np == lhsb) {
  1230.         ungetC(HIST);
  1231.         return (0);
  1232.         }
  1233.         *np++ = 0;
  1234.         if (event != -1) {
  1235.         /*
  1236.          * History had only digits
  1237.          */
  1238.         if (back)
  1239.             event = eventno + (alhistp == 0) - (event ? event : 0);
  1240.         break;
  1241.         }
  1242.         if (back) {
  1243.         event = sizeof(lhsb) / sizeof(lhsb[0]);
  1244.         np = &lhsb[--event];
  1245.         *np-- = '\0';
  1246.         for (event--; np > lhsb; *np-- = lhsb[--event]);
  1247.         *np = '-';
  1248.         }
  1249.         hp = findev(lhsb, 0);
  1250.         if (hp)
  1251.         lastev = hp->Hnum;
  1252.         return (&hp->Hlex);
  1253.  
  1254.     case '?':
  1255.         np = lhsb;
  1256.         for (;;) {
  1257.         c = getC(0);
  1258.         if (c == '\n') {
  1259.             unreadc(c);
  1260.             break;
  1261.         }
  1262.         if (c == '?')
  1263.             break;
  1264.         if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
  1265.             *np++ = c;
  1266.         }
  1267.         if (np == lhsb) {
  1268.         if (lhsb[0] == 0) {
  1269.             seterror(ERR_NOSEARCH);
  1270.             return (0);
  1271.         }
  1272.         }
  1273.         else
  1274.         *np++ = 0;
  1275.         hp = findev(lhsb, 1);
  1276.         if (hp)
  1277.         lastev = hp->Hnum;
  1278.         return (&hp->Hlex);
  1279.     }
  1280.  
  1281.     for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
  1282.     if (hp->Hnum == event) {
  1283.         hp->Href = eventno;
  1284.         lastev = hp->Hnum;
  1285.         return (&hp->Hlex);
  1286.     }
  1287.     np = putn(event);
  1288.     seterror(ERR_NOEVENT, short2str(np));
  1289.     return (0);
  1290. }
  1291.  
  1292. static struct Hist *
  1293. findev(cp, anyarg)
  1294.     Char   *cp;
  1295.     bool    anyarg;
  1296. {
  1297.     register struct Hist *hp;
  1298.  
  1299.     for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
  1300.     Char   *dp;
  1301.     register Char *p, *q;
  1302.     register struct wordent *lp = hp->Hlex.next;
  1303.     int     argno = 0;
  1304.  
  1305.     /*
  1306.      * The entries added by alias substitution don't have a newline but do
  1307.      * have a negative event number. Savehist() trims off these entries,
  1308.      * but it happens before alias expansion, too early to delete those
  1309.      * from the previous command.
  1310.      */
  1311.     if (hp->Hnum < 0)
  1312.         continue;
  1313.     if (lp->word[0] == '\n')
  1314.         continue;
  1315.     if (!anyarg) {
  1316.         p = cp;
  1317.         q = lp->word;
  1318.         do
  1319.         if (!*p)
  1320.             return (hp);
  1321.         while (*p++ == *q++);
  1322.         continue;
  1323.     }
  1324.     do {
  1325.         for (dp = lp->word; *dp; dp++) {
  1326.         p = cp;
  1327.         q = dp;
  1328.         do
  1329.             if (!*p) {
  1330.             quesarg = argno;
  1331.             return (hp);
  1332.             }
  1333.         while (*p++ == *q++);
  1334.         }
  1335.         lp = lp->next;
  1336.         argno++;
  1337.     } while (lp->word[0] != '\n');
  1338.     }
  1339.     seterror(ERR_NOEVENT, short2str(cp));
  1340.     return (0);
  1341. }
  1342.  
  1343.  
  1344. static void
  1345. setexclp(cp)
  1346.     register Char *cp;
  1347. {
  1348.     if (cp && cp[0] == '\n')
  1349.     return;
  1350.     exclp = cp;
  1351. }
  1352.  
  1353. void
  1354. unreadc(c)
  1355.     int    c;
  1356. {
  1357.     peekread = c;
  1358. }
  1359.  
  1360. int
  1361. readc(wanteof)
  1362.     bool    wanteof;
  1363. {
  1364.     register int c;
  1365.     static  sincereal;
  1366.  
  1367. #ifdef DEBUG_INP
  1368.     xprintf("readc\n");
  1369. #endif
  1370.     if (c = peekread) {
  1371.     peekread = 0;
  1372.     return (c);
  1373.     }
  1374. top:
  1375.     aret = F_SEEK;
  1376.     if (alvecp) {
  1377. #ifdef DEBUG_INP
  1378.     xprintf("alvecp %c\n", *alvecp & 0xff);
  1379. #endif
  1380.     aret = A_SEEK;
  1381.     if (c = *alvecp++)
  1382.         return (c);
  1383.     if (alvec && *alvec) {
  1384.         alvecp = *alvec++;
  1385.         return (' ');
  1386.     }
  1387.     else {
  1388.         alvecp = NULL;
  1389.         aret = F_SEEK;
  1390.         return('\n');
  1391.     }
  1392.     }
  1393.     if (alvec) {
  1394.     if (alvecp = *alvec) {
  1395.         alvec++;
  1396.         goto top;
  1397.     }
  1398.     /* Infinite source! */
  1399.     return ('\n');
  1400.     }
  1401.     if (evalp) {
  1402.     aret = E_SEEK;
  1403.     if (c = *evalp++)
  1404.         return (c);
  1405.     if (evalvec && *evalvec) {
  1406.         evalp = *evalvec++;
  1407.         return (' ');
  1408.     }
  1409.     aret = F_SEEK;
  1410.     evalp = 0;
  1411.     }
  1412.     if (evalvec) {
  1413.     if (evalvec == (Char **) 1) {
  1414.         doneinp = 1;
  1415.         reset();
  1416.     }
  1417.     if (evalp = *evalvec) {
  1418.         evalvec++;
  1419.         goto top;
  1420.     }
  1421.     evalvec = (Char **) 1;
  1422.     return ('\n');
  1423.     }
  1424.     do {
  1425.     if (arginp == (Char *) 1 || onelflg == 1) {
  1426.         if (wanteof)
  1427.         return (-1);
  1428.         exitstat();
  1429.     }
  1430.     if (arginp) {
  1431.         if ((c = *arginp++) == 0) {
  1432.         arginp = (Char *) 1;
  1433.         return ('\n');
  1434.         }
  1435.         return (c);
  1436.     }
  1437. reread:
  1438.     c = bgetc();
  1439.     if (c < 0) {
  1440. #ifndef POSIX
  1441. # ifdef TERMIO
  1442.         struct termio tty;
  1443. # else /* SGTTYB */
  1444.         struct sgttyb tty;
  1445. # endif /* TERMIO */
  1446. #else /* POSIX */
  1447.         struct termios tty;
  1448. #endif /* POSIX */
  1449.         if (wanteof)
  1450.         return (-1);
  1451.         /* was isatty but raw with ignoreeof yields problems */
  1452. #ifndef POSIX
  1453. # ifdef TERMIO
  1454.         if (ioctl(SHIN, TCGETA, (ioctl_t) & tty) == 0 &&
  1455.         (tty.c_lflag & ICANON))
  1456. # else /* GSTTYB */
  1457.         if (ioctl(SHIN, TIOCGETP, (ioctl_t) & tty) == 0 &&
  1458.         (tty.sg_flags & RAW) == 0)
  1459. # endif /* TERMIO */
  1460. #else /* POSIX */
  1461.         if (tcgetattr(SHIN, &tty) == 0 &&
  1462.         (tty.c_lflag & ICANON))
  1463. #endif /* POSIX */
  1464.         {
  1465.         /* was 'short' for FILEC */
  1466.         int     ctpgrp;
  1467.  
  1468.         if (++sincereal > 25)
  1469.             goto oops;
  1470. #ifdef BSDJOBS
  1471.         if (tpgrp != -1 &&
  1472.             (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
  1473.             tpgrp != ctpgrp) {
  1474.             (void) tcsetpgrp(FSHTTY, tpgrp);
  1475.             (void) killpg((pid_t) ctpgrp, SIGHUP);
  1476.             xprintf("Reset tty pgrp from %d to %d\n", ctpgrp, tpgrp);
  1477.             goto reread;
  1478.         }
  1479. #endif /* BSDJOBS */
  1480.         if (adrof(STRignoreeof)) {
  1481.             if (loginsh)
  1482.             xprintf("\nUse \"logout\" to logout.\n");
  1483.             else
  1484.             xprintf("\nUse \"exit\" to leave tcsh.\n");
  1485.             reset();
  1486.         }
  1487.         if (chkstop == 0)
  1488.             panystop(1);
  1489.         }
  1490.     oops:
  1491.         doneinp = 1;
  1492.         reset();
  1493.     }
  1494.     sincereal = 0;
  1495.     if (c == '\n' && onelflg)
  1496.         onelflg--;
  1497.     } while (c == 0);
  1498.     if (histlinep < histline + BUFSIZE)
  1499.     *histlinep++ = c;
  1500.     return (c);
  1501. }
  1502.  
  1503. static int
  1504. bgetc()
  1505. {
  1506.     register int buf, off;
  1507.     int c;
  1508.     register int numleft = 0, roomleft;
  1509.     extern Char InputBuf[];
  1510.     char    tbuf[BUFSIZE + 1];
  1511.  
  1512.     if (cantell) {
  1513.     if (fseekp < fbobp || fseekp > feobp) {
  1514.         fbobp = feobp = fseekp;
  1515.         (void) lseek(SHIN, fseekp, L_SET);
  1516.     }
  1517.     if (fseekp == feobp) {
  1518.         int     i;
  1519.  
  1520.         fbobp = feobp;
  1521.         do
  1522.         c = read(SHIN, tbuf, BUFSIZE);
  1523.         while (c < 0 && errno == EINTR);
  1524.         if (c <= 0)
  1525.         return (-1);
  1526.         for (i = 0; i < c; i++)
  1527.         fbuf[0][i] = (unsigned char) tbuf[i];
  1528.         feobp += c;
  1529.     }
  1530.     c = fbuf[0][fseekp - fbobp];
  1531.     fseekp++;
  1532.     return (c);
  1533.     }
  1534. again:
  1535.     buf = (int) fseekp / BUFSIZE;
  1536.     if (buf >= fblocks) {
  1537.     register Char **nfbuf =
  1538.     (Char **) xcalloc((size_t) (fblocks + 2),
  1539.               sizeof(Char **));
  1540.  
  1541.     if (fbuf) {
  1542.         (void) blkcpy(nfbuf, fbuf);
  1543.         xfree((ptr_t) fbuf);
  1544.     }
  1545.     fbuf = nfbuf;
  1546.     fbuf[fblocks] = (Char *) xcalloc(BUFSIZE, sizeof(Char));
  1547.     fblocks++;
  1548.     if (!intty)
  1549.         goto again;
  1550.     }
  1551.     if (fseekp >= feobp) {
  1552.     buf = (int) feobp / BUFSIZE;
  1553.     off = (int) feobp % BUFSIZE;
  1554.     roomleft = BUFSIZE - off;
  1555.     for (;;) {
  1556.         if (editing && intty) {    /* then use twenex routine */
  1557.         c = numleft ? numleft : Inputl();    /* PWP: get a line */
  1558.         if (c > roomleft) {    /* No room in this buffer? */
  1559.             /* start with fresh buffer */
  1560.             feobp = fseekp = fblocks * BUFSIZE;
  1561.             numleft = c;
  1562.             goto again;
  1563.         }
  1564.         if (c > 0)
  1565.             copy((char *) (fbuf[buf] + off),
  1566.              (char *) InputBuf, (int) (c * sizeof(Char)));
  1567.         /* copy (fbuf[buf] + off, ttyline, c); */
  1568.         numleft = 0;
  1569.         }
  1570.         else {
  1571.         c = read(SHIN, tbuf, (size_t) roomleft);
  1572.         if (c > 0) {
  1573.             int     i;
  1574.             Char   *ptr = fbuf[buf] + off;
  1575.  
  1576.             for (i = 0; i < c; i++)
  1577.             ptr[i] = (unsigned char) tbuf[i];
  1578.         }
  1579.         }
  1580.         if (c >= 0)
  1581.         break;
  1582.         switch (errno) {
  1583. #ifdef EWOULDBLOCK
  1584.         case EWOULDBLOCK:
  1585. # define TRY_AGAIN
  1586. #endif /* EWOULDBLOCK */
  1587. #if defined(POSIX) && defined(EAGAIN)
  1588. # if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
  1589.         case EAGAIN:
  1590. #  define TRY_AGAIN
  1591. # endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
  1592. #endif /* POSIX && EAGAIN */
  1593. #ifdef TRY_AGAIN
  1594. # if defined(F_SETFL) && defined(O_NDELAY)
  1595.         (void) fcntl(SHIN, F_SETFL, fcntl(SHIN,F_GETFL,0) & ~O_NDELAY);
  1596. # endif /* F_SETFL && O_NDELAY */
  1597. # ifdef FIONBIO
  1598.         c = 0;
  1599.         (void) ioctl(SHIN, FIONBIO, (ioctl_t) &c);
  1600. # endif    /* FIONBIO */
  1601. # if (defined(F_SETFL) && defined(O_NDELAY)) || defined(FIONBIO)
  1602.         c = 0;
  1603. # endif    /* (F_SETFL && O_NDELAY) || FIONBIO */
  1604.         break;
  1605. #endif /* TRY_AGAIN */
  1606.         case EINTR:
  1607.         c = 0;
  1608.         break;
  1609.         default:
  1610.         c = -1;
  1611.         break;
  1612.         }
  1613.         if (c == -1)
  1614.         break;
  1615.     }
  1616.     if (c <= 0)
  1617.         return (-1);
  1618.     feobp += c;
  1619.     if (editing && !intty)
  1620.         goto again;
  1621.     }
  1622.     c = fbuf[buf][(int) fseekp % BUFSIZE];
  1623.     fseekp++;
  1624.     return (c);
  1625. }
  1626.  
  1627. static void
  1628. bfree()
  1629. {
  1630.     register int sb, i;
  1631.  
  1632.     if (cantell)
  1633.     return;
  1634.     if (whyles)
  1635.     return;
  1636.     sb = (int) (fseekp - 1) / BUFSIZE;
  1637.     if (sb > 0) {
  1638.     for (i = 0; i < sb; i++)
  1639.         xfree((ptr_t) fbuf[i]);
  1640.     (void) blkcpy(fbuf, &fbuf[sb]);
  1641.     fseekp -= BUFSIZE * sb;
  1642.     feobp -= BUFSIZE * sb;
  1643.     fblocks -= sb;
  1644.     }
  1645. }
  1646.  
  1647. void
  1648. bseek(l)
  1649.     struct Ain   *l;
  1650. {
  1651.     switch (aret = l->type) {
  1652.     case E_SEEK:
  1653.     evalvec = l->a_seek;
  1654.     evalp = (Char *) l->f_seek;
  1655. #ifdef DEBUG_SEEK
  1656.     xprintf("seek to eval %x %x\n", evalvec, evalp);
  1657. #endif
  1658.     return;
  1659.     case A_SEEK:
  1660.     alvec = l->a_seek;
  1661.     alvecp = (Char *) l->f_seek;
  1662. #ifdef DEBUG_SEEK
  1663.     xprintf("seek to alias %x %x\n", alvec, alvecp);
  1664. #endif
  1665.     return;
  1666.     case F_SEEK:    
  1667. #ifdef DEBUG_SEEK
  1668.     xprintf("seek to file %x\n", fseekp);
  1669. #endif
  1670.     fseekp = l->f_seek;
  1671.     return;
  1672.     default:
  1673.     xprintf("Bad seek type %d\n", aret);
  1674.     abort();
  1675.     }
  1676. }
  1677.  
  1678. /* any similarity to bell telephone is purely accidental */
  1679. void
  1680. btell(l)
  1681. struct Ain *l;
  1682. {
  1683.     switch (l->type = aret) {
  1684.     case E_SEEK:
  1685.     l->a_seek = evalvec;
  1686.     l->f_seek = (off_t) evalp;
  1687. #ifdef DEBUG_SEEK
  1688.     xprintf("tell eval %x %x\n", evalvec, evalp);
  1689. #endif
  1690.     return;
  1691.     case A_SEEK:
  1692.     l->a_seek = alvec;
  1693.     l->f_seek = (off_t) alvecp;
  1694. #ifdef DEBUG_SEEK
  1695.     xprintf("tell alias %x %x\n", alvec, alvecp);
  1696. #endif
  1697.     return;
  1698.     case F_SEEK:
  1699.     l->f_seek = fseekp;
  1700.     l->a_seek = NULL;
  1701. #ifdef DEBUG_SEEK
  1702.     xprintf("tell file %x\n", fseekp);
  1703. #endif
  1704.     return;
  1705.     default:
  1706.     xprintf("Bad seek type %d\n", aret);
  1707.     abort();
  1708.     }
  1709. }
  1710.  
  1711. void
  1712. btoeof()
  1713. {
  1714.     (void) lseek(SHIN, (off_t) 0, L_XTND);
  1715.     aret = F_SEEK;
  1716.     fseekp = feobp;
  1717.     alvec = NULL;
  1718.     alvecp = NULL;
  1719.     evalvec = NULL;
  1720.     evalp = NULL;
  1721.     wfree();
  1722.     bfree();
  1723. }
  1724.  
  1725. void
  1726. settell()
  1727. {
  1728.     cantell = 0;
  1729.     if (arginp || onelflg || intty)
  1730.     return;
  1731.     if (lseek(SHIN, (off_t) 0, L_INCR) < 0 || errno == ESPIPE)
  1732.     return;
  1733.     fbuf = (Char **) xcalloc(2, sizeof(Char **));
  1734.     fblocks = 1;
  1735.     fbuf[0] = (Char *) xcalloc(BUFSIZE, sizeof(Char));
  1736.     fseekp = fbobp = feobp = lseek(SHIN, (off_t) 0, L_INCR);
  1737.     cantell = 1;
  1738. }
  1739.